package com.hiof.heartrate;

import com.hiof.Configuration;
import com.hiof.antplus.AntPlusManager;
import com.hiof.utility.Utility;

/**
 * Created with IntelliJ IDEA.
 * User: manish
 * Date: 5/26/12
 * Time: 6:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class AntPlusHeartRateManager extends HeartRateManager implements AntPlusManager.Callbacks {
    private AntPlusManager antManager;
    private static AntPlusHeartRateManager instance;

    public static AntPlusHeartRateManager getInstance() {
        if (null == instance) {
            instance = new AntPlusHeartRateManager();
        }
        return instance;
    }

    private AntPlusHeartRateManager() {
        initAntPlusManager();
    }

    private void initAntPlusManager() {
        //TODO: Make it fixed
        Utility.debug(this.getClass().getName(), "Initializing AntPlus manager");
        antManager = new AntPlusManager();
        //test code
        boolean started = antManager.start(Configuration.getInstance().getCurrentContext());
        antManager.doEnable();
        antManager.openChannel(AntPlusManager.HRM_CHANNEL, true);
    }

    @Override
    public double getLatestHeartRate() {
        return antManager.getBPM();
    }

    @Override
    public HealthStatus getHealthStatus() {
        //TODO: algorithms to calculate the heal status
        //Get from MoraService list of heart rate (sort by timestamp) --> algorithms to determine it
        // table should be | userId | athleteId | timestamp | hearRate
        // hearRateList[size - 1] > threshold
        return null;
    }

    @Override
    public void destroy() {
        if (antManager.isEnabled()){
            antManager.doDisable();
            antManager = null;
        }
        if(antManager!=null){
            antManager = null;
        }
        if(instance!=null){
            instance = null;
        }
    }

    @Override
    public void errorCallback() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void notifyAntStateChanged() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void notifyChannelStateChanged(byte channel) {
        switch (antManager.getHrmState()) {
            case CLOSED:
                break;
            case OFFLINE:
                break;
            case SEARCHING:
                break;
            case PENDING_OPEN:
                break;
            case TRACKING_STATUS:
                break;
            case TRACKING_DATA:
                onHearRateChanged(antManager.getBPM());
                break;
        }
    }

    @Override
    public void notifyChannelDataChanged(byte channel) {
        if (channel == AntPlusManager.HRM_CHANNEL) {
            switch (antManager.getHrmState()) {
                case CLOSED:
                case OFFLINE:
                case SEARCHING:
                case PENDING_OPEN:
                case TRACKING_STATUS:
                case TRACKING_DATA:
                    onHearRateChanged(antManager.getBPM());
                    break;
            }
        }
    }
}
